﻿//
// Name: Form1.cs
// Project: elektor HiD Temperature Modul
// Author: Michael Odenwald
// Creation Date: 10.05.2013 
//

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace WindowsApp
{
    public partial class Form1 : Form
    {
        // Create an instance of the USB reference device
        private usbDevice theUsbDevice;
        private String HiDKey = "SOFTWARE\\elektor\\Temperature Modul";

        /// <summary>
        /// This is the application for the elektor USB HiD Temperature Modul with the 
        /// usbGenericHidCommunications class library for communication.  
        /// </summary>
        public Form1()
        {
            InitializeComponent();

            // Create the USB reference device object (passing VID and PID)
            theUsbDevice = new usbDevice(0x0C7D, 0x0011);

            // Add a listener for the Form closing event
            this.FormClosing += this.Form1_FormClosing;

            // Add a listener for usb events
            theUsbDevice.usbEvent += new usbDevice.usbEventsHandler(usbEvent_receiver);

            // Perform an initial search for the target device
            theUsbDevice.findTargetDevice();
        }

        //
        // SubKeyExist
        // 
        // check the existing of a subkey
        //
        private bool SubKeyExist(string Subkey)
        {
            // Check if a Subkey exist
            RegistryKey myKey = Registry.CurrentUser.OpenSubKey(Subkey);
            if (myKey == null)
                return false;
            else
                return true;
        }

        //
        // Form1_FormClosing
        // 
        // Close the form and save parameter to the registry
        //
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            RegistryKey HiDTempKey = Registry.CurrentUser.CreateSubKey(HiDKey);

            if (((int)this.Location.X > 0) && ((int)this.Location.Y > 0)) // write only if Pos is in visiable area
            {
                // Write Values to the Subkey
                HiDTempKey.SetValue("X-Pos", this.Location.X.ToString());
                HiDTempKey.SetValue("Y-Pos", this.Location.Y.ToString());
            }
        }

        //
        // usbEvent_receiver
        // 
        // receiver for the USB events attached / detached
        //
        private void usbEvent_receiver(object o, EventArgs e)
        {
            // Check the status of the USB device and update the form accordingly
            if (theUsbDevice.isDeviceAttached)
            {
                // Device is currently attached

                // Update the status label
                this.toolStripStatusLabel1.Text = "USB Device is attached";
                Double tval = theUsbDevice.GetTemperatur();
                this.label1.Text = string.Format("{0:0.0000} °C", tval / 10000);

                // enable the timer
                this.timer1.Enabled = true;
            }
            else
            {
                // Device is currently unattached

                // Update the status label
                this.toolStripStatusLabel1.Text = "USB Device is detached";
                this.label1.Text = "  ---  °C";

                // disable the timer
                this.timer1.Enabled = false;
            }
        }

        //
        // timer1_Tick
        // 
        // call when the timer event is triggered
        //
        private void timer1_Tick(object sender, EventArgs e)
        {
            Double tval = theUsbDevice.GetTemperatur();
            this.label1.Text = string.Format("{0:0.0000} °C", tval / 10000);
        }

        //
        // Form1_Load
        // 
        // Load the form and set the position
        //
        private void Form1_Load(object sender, EventArgs e)
        {
            RegistryKey HiDTempKey = Registry.CurrentUser.OpenSubKey(HiDKey);
            Int32 X;
            Int32 Y;
            
            if (SubKeyExist(HiDKey))
            {
                X = Convert.ToInt32(HiDTempKey.GetValue("X-Pos"));
                Y = Convert.ToInt32(HiDTempKey.GetValue("Y-Pos"));

                if ((X > 0) && (Y > 0)) // value are in visiable area?
                {
                    this.Location = new Point(X, Y);
                }
                else
                {
                    this.Location = new Point(10, 10);
                }
            }
            else
            {
                this.Location = new Point(10, 10); // no value in the registry, than using position 10, 10
            }
        }

    }
}
